<template>
  <basic-crud :option="option" ref="basicCrudRef">
    <template #menu="{ row }">
      <el-button
        icon="Promotion"
        plain
        type="primary"
        size="small"
        :loading="row.loading"
        @click="handleGenerate(row)"
        >生成</el-button
      >
      <el-button
        icon="View"
        plain
        type="primary"
        size="small"
        @click="handlePreview(row)"
        >预览</el-button
      >
    </template>
  </basic-crud>
  <CodePreview v-model="visible" v-model:code="code"></CodePreview>
</template>

<script setup>
import { apiGetBusTypeDialogList } from '@/apis/objectModel/businessType.js'
import { apiDownFile } from '../../../apis/global'
import { apiCodeProduce } from '@/apis/objectModel/codeGenerator.js'
import CodePreview from './components/CodePreview.vue'
const basicCrudRef = ref()
const option = {
  viewBtn: true,
  searchSpan: 6,
  menuWidth: 400,
  column: [
    {
      label: '业务类型名称',
      prop: 'name',
      width: 200,
      search: true,
      sortable: true,
      rules: [
        { required: true, message: '业务类型名称不能为空', trigger: 'change' },
      ],
      component: 'BasicCrudTableSelect',
      crudProps: {
        width: '60%',
        apiSource: { apiGetList: apiGetBusTypeDialogList },
      },
      comProps: {
        customizeFun: (type, row) => {
          let model = {}
          if (type === 'save') {
            var packageName = row.classFullName?.substring(
              0,
              row.classFullName.lastIndexOf('.')
            )
            model['doPackage'] = packageName || ''
            model['name'] = row.name || ''
            model['code'] = row.code || ''
            model['parentField'] = row.parentField || ''

            if (packageName.endsWith('.model')) {
              model['voPackage'] = packageName.replace('.model', '.vo')
              model['dtoPackage'] = packageName.replace('.model', '.dto')
              model['controllerPackage'] = packageName.replace(
                '.model',
                '.controller'
              )
              model['servicePackage'] = packageName.replace(
                '.model',
                '.service'
              )
              model['mapperPackage'] = packageName.replace('.model', '.mapper')
              model['providerPackage'] = packageName.replace(
                '.model',
                '.provider'
              )
              var className = row.classFullName?.substring(
                row.classFullName.lastIndexOf('.') + 1
              )
              model['className'] = className
              if (className.endsWith('DO')) {
                className = className.substring(0, className.length - 2)
              }
              model['controllerRequestMap'] =
                '/' +
                className.substring(0, 1).toLowerCase() +
                className.substring(1) +
                'Controller'
            }
          } else {
            model = {
              doPackage: undefined,
              name: undefined,
              code: undefined,
              voPackage: undefined,
              dtoPackage: undefined,
              controllerPackage: undefined,
              servicePackage: undefined,
              mapperPackage: undefined,
              providerPackage: undefined,
              className: undefined,
              controllerRequestMap: undefined,
              parentField: undefined,
            }
          }
          return model
        },
        saveFieldMap: ['code', 'name'],
        selectedKey: 'code',
      },
    },
    {
      label: '业务类型编号',
      prop: 'code',
      search: true,
      width: 200,
      sortable: true,
      rules: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
      disabled: true,
    },
    {
      label: '实体类名称',
      width: 180,
      prop: 'className',
    },
    {
      label: '数据对象的包名',
      width: 180,
      prop: 'doPackage',
    },
    {
      label: '显示对象的包名',
      prop: 'voPackage',
      hide: true,
      display: true,
    },
    {
      label: '数据传输对象',
      prop: 'dtoPackage',
      hide: true,
      display: true,
    },
    {
      label: '控制器的包名',
      prop: 'controllerPackage',
      hide: true,
      display: true,
    },
    {
      label: '服务包名',
      prop: 'servicePackage',
      hide: true,
      display: true,
    },
    {
      label: '数据库操作包名',
      prop: 'mapperPackage',
      hide: true,
      display: true,
    },
    {
      label: '数据库xml包名',
      prop: 'mapperXmlPackage',
      hide: true,
      display: true,
    },
    {
      label: '最后生成时间',
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      prop: 'lastProductDate',
      width: 180,
    },
    {
      label: '上下级关系',
      prop: 'parentField',
      hide: true,
      display: true,
    },
    {
      label: '服务编号',
      prop: 'serviceCode',
      hide: true,
      display: true,
    },
    {
      label: '描述',
      prop: 'des',
      type: 'textarea',
      span: 24,
    },
  ],
}
const handleGenerate = async (row) => {
  row.loading = true
  try {
    await apiCodeProduce(row.code)
    apiDownFile({
      url: '/obj/genSchemaController/download',
      method: 'get',
      params: { code: row.code },
    })
  } finally {
    row.loading = false
  }
}

const visible = ref(false)
const code = ref('')

const handlePreview = (row) => {
  code.value = row.code
  visible.value = true
}
</script>

<style lang="scss" scoped></style>
